package net.zhengxinyun.performance.data;

import com.alibaba.fastjson.JSONArray;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.Util.WorkbookUtil;
import net.zhengxinyun.performance.entity.WorkScoreEntity;
import net.zhengxinyun.performance.entity.WorkSupervisionEntity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/12.
 */
@Component
public class WorkSupervisionData {

    private static final String COL_INDEX_NAME = "指标名称";
    private static final String COL_MAX_POINT = "封顶扣分限值";
    private static final String COL_CITY_RESPON_UNIT = "市考核责任单位";
    private static final String COL_COOPERATE_UNIT = "考核配合单位";
    private static final String COL_LEAD_UNIT = "工作目标牵头单位";
    private static final String COL_DATE_TIME="指标年份";
    private static final String COL_SCORE="得分";
    private static final String COL_MONTH_TIME="月份";

    private static final String[] LABLES_ZERO = {"指标名称", "封顶扣分限值", "市考核责任单位", "考核配合单位", "工作目标牵头单位","指标年份","得分","月份"};

    public List<WorkSupervisionEntity> importWorkSupervision(String filePath) throws Exception {
        List<WorkSupervisionEntity> result = new ArrayList<WorkSupervisionEntity>();
        List<String> errors = new ArrayList<String>();
        Workbook book = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = book.getSheetAt(0);
        Iterator<Cell> iteratorZero = sheet.getRow(0).cellIterator();
        int length = 0;
        List<String> lablesListZero = new ArrayList<String>();
        while (iteratorZero.hasNext()) {
            String str = iteratorZero.next().getStringCellValue();
            if (!"".equals(str)) {
                lablesListZero.add(str);
                if (length > 7) {
                    throw new Exception("请选择正确模板上传");
                }
                length++;
            }
        }
        if (!Util.arrayEquals(lablesListZero.toArray(), LABLES_ZERO)) {
            throw new Exception("请选择正确模板上传");
        }
        for (int rowIdx = 1; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)) {
                continue;
            }
            try {
                WorkSupervisionEntity entity = new WorkSupervisionEntity();

                String indexName = WorkbookUtil.getStringCellValue(row,COL_INDEX_NAME);
                if (StringUtils.isBlank(indexName)){
                    throw new Exception("指标名称不能为空");
                }else {
                    entity.setIndexName(indexName);
                }
                String maxPoint = WorkbookUtil.getStringCellValue(row,COL_MAX_POINT);
                if (StringUtils.isBlank(maxPoint)){
                    throw new Exception("封顶扣分限值不能为空");
                }else {
                    entity.setMaxPoint(maxPoint);
                }
                String cityResponUnit = WorkbookUtil.getStringCellValue(row,COL_CITY_RESPON_UNIT);
                if (StringUtils.isBlank(cityResponUnit)){
                    throw new Exception("市考核责任单位不能为空");
                }else {
                    entity.setCityResponUnit(cityResponUnit);
                }
                String cooperateUnit = WorkbookUtil.getStringCellValue(row,COL_COOPERATE_UNIT);
                entity.setCooperateUnit(cooperateUnit);
                String leadUnit = WorkbookUtil.getStringCellValue(row,COL_LEAD_UNIT);
                if (StringUtils.isBlank(leadUnit)){
                    throw new Exception("工作目标牵头单位不能为空");
                }else {
                    entity.setLeadUnit(leadUnit);
                }
                String dateTime = WorkbookUtil.getStringCellValue(row,COL_DATE_TIME);
                if (StringUtils.isBlank(dateTime)){
                    throw new Exception("指标年份不能为空");
                }else {
                    entity.setDateTime(dateTime);
                }
                result.add(entity);
            }catch (Exception e){
                errors.add("第" + rowIdx + "行：" + e.getMessage());
            }
        }
        if (errors.isEmpty())
            throw new Exception(JSONArray.toJSONString(errors));
        return result;
    }

    public List<WorkScoreEntity> importWorkPoint(String filePath) throws Exception{
        List<WorkScoreEntity> result = new ArrayList<WorkScoreEntity>();
        List<String> errors = new ArrayList<String>();
        Workbook book = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = book.getSheetAt(0);
        Iterator<Cell> iteratorZero = sheet.getRow(0).cellIterator();
        int length = 0;
        List<String> lablesListZero = new ArrayList<String>();
        while (iteratorZero.hasNext()) {
            String str = iteratorZero.next().getStringCellValue();
            if (!"".equals(str)) {
                lablesListZero.add(str);
                if (length > 7) {
                    throw new Exception("请选择正确模板上传");
                }
                length++;
            }
        }
        if (!Util.arrayEquals(lablesListZero.toArray(), LABLES_ZERO)) {
            throw new Exception("请选择正确模板上传");
        }
        for (int rowIdx = 1; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)) {
                continue;
            }
            try {
                WorkScoreEntity entity = new WorkScoreEntity();

                String indexName = WorkbookUtil.getStringCellValue(row,COL_INDEX_NAME);
                if (StringUtils.isBlank(indexName)){
                    throw new Exception("指标名称不能为空");
                }else {
                    entity.setIndicatorsId(indexName);
                }

                String dateTime = WorkbookUtil.getStringCellValue(row,COL_DATE_TIME);
                if (StringUtils.isBlank(dateTime)){
                    throw new Exception("指标年份不能为空");
                }else {
                    entity.setDateTime(dateTime);
                }
                String score = WorkbookUtil.getStringCellValue(row,COL_SCORE);
                if (StringUtils.isBlank(score)){
                    throw new Exception("得分不能为空");
                }else {
                    entity.setScore(Double.valueOf(score));
                }
                String monthTime = WorkbookUtil.getStringCellValue(row,COL_MONTH_TIME);
                if (StringUtils.isBlank(monthTime)){
                    throw new Exception("月份不能为空");
                }else {
                    if (monthTime.length() == 1){
                        entity.setMonthTime("0"+monthTime);
                    }else {
                        entity.setMonthTime(monthTime);
                    }
                }
                entity.setAudit("2");
                result.add(entity);
            }catch (Exception e){
                errors.add("第" + rowIdx + "行：" + e.getMessage());
            }
        }
        if (errors.isEmpty() == false)
            throw new Exception(JSONArray.toJSONString(errors));
        return result;
    }




}
